/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package izsla.beans;

import izsla.entities.TripUser;
import izsla.sb.UserManagementLocal;
import javax.ejb.EJB;
import javax.faces.context.FacesContext;

/**
 *
 * @author aki
 */
public class Login {
    // REQ2
    private String error;
    private String name;
    private String password;

    @EJB
    private UserManagementLocal um;

    public Login() {
	um = UserManagementSingleton.get();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String user) {
        this.name = user;
    }

    public String getError() {
        return error;
    }

    /**
     * If the login information is correct, the user is directed to the "Trips" page.
     */
    public String onLogin() {
        TripUser u = um.login(name, password);
        if(u == null) {
	    error = "Invalid username or password!";
            return null;
	}

	FacesContext fc = FacesContext.getCurrentInstance();
	fc.getExternalContext().getSessionMap().put("user", u);

        return "search";
    }

    public String onLogout() {
	FacesContext fc = FacesContext.getCurrentInstance();
	fc.getExternalContext().getSessionMap().remove("user");

	return "login";
    }
}
